package ar.uade.distribuidas.tpo.web.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.NotBoundException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import ar.uade.distribuidas.tpo.base.dominiodto.CotizacionDTO;
import ar.uade.distribuidas.tpo.base.dominiodto.ItemCotizacionDTO;
import ar.uade.distribuidas.tpo.base.dominiodto.ItemPedidoDTO;
import ar.uade.distribuidas.tpo.base.dominiodto.OrdenDePedidoDTO;
import ar.uade.distribuidas.tpo.dao.OrdenDePedidoXmlDao;
import ar.uade.distribuidas.tpo.web.delegado.BD;



/**
 * Servlet implementation class OrdenPedido
 */
@WebServlet("/OrdenPedido")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10,      // 10MB
maxRequestSize=1024*1024*50)   // 50MB

public class OrdenPedido extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrdenPedido() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/ordenpedido.jsp");
		//Long cuil = Long.valueOf(request.getParameter("cuil"));
		//String archivo = request.getParameter("file");
		
		Part parteArch = request.getPart("file");
		 InputStream filecontent = parteArch.getInputStream();
		
		
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(filecontent);
			
			OrdenDePedidoXmlDao oxml = OrdenDePedidoXmlDao.getInstancia();
			List<OrdenDePedidoDTO> lop = oxml.getOrdenDePedidoDTO(doc);
			
			for (OrdenDePedidoDTO op : lop){
				for (ItemPedidoDTO item : op.getItemsPedido()){
					CotizacionDTO cot = BD.getInstance().getCotizacion(item.getCotizacionId());
					for (ItemCotizacionDTO icot : cot.getItemCotizacionDTO()){
						item.setItemCotizacionId(icot.getId());
					}
				}
			}
			
			BD.getInstance().altaListaDeOrdenesDePedido(lop);
			
			request.setAttribute("allok", "La solicitud fue procesada correctamente.");
			request.getRequestDispatcher("/jsp/ordenpedido.jsp").forward(request, response);
			
		} catch (NotBoundException e) {
			request.setAttribute("allerror", "Hubo un error al procesar la solicitud.");
			request.getRequestDispatcher("/jsp/ordenpedido.jsp").forward(request, response);
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	}

}
